package com.android.yinghebike.ui.activity;

import android.text.TextUtils;
import android.view.View;

import com.android.baselibrary.tool.CommToast;
import com.android.baselibrary.view.Comm_EditView;
import com.android.baselibrary.view.Comm_HeadView;
import com.android.yinghebike.R;
import com.android.yinghebike.bean.UserInfo;

import org.litepal.LitePal;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class RegistActivity extends BaseActivity {

    @BindView(R.id.comm_title)
    Comm_HeadView comm_title;
    @BindView(R.id.et_account)
    Comm_EditView et_account;
    @BindView(R.id.et_pwd)
    Comm_EditView et_pwd;

    List<UserInfo> userInfos;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_regist;
    }

    @Override
    protected void initView() {
        comm_title.setTitle(getString(R.string.login_regist));

    }

    @Override
    protected void initData() {
        userInfos = LitePal.findAll(UserInfo.class);


    }

    @OnClick({R.id.tv_regist})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_regist:
                regist();
                break;
        }
    }

    private void regist() {
        String account = et_account.getText();
        String pwd = et_pwd.getText();
        if (TextUtils.isEmpty(account)) {
            CommToast.showToast(mContext, getString(R.string.login_hint_account));
            return;
        }
        if (TextUtils.isEmpty(pwd)) {
            CommToast.showToast(mContext, getString(R.string.login_hint_pwd));
            return;
        }
        //判断是否已注册
        for (UserInfo userInfo : userInfos) {
            if (userInfo.account.equals(account)) {
                CommToast.showToast(mContext, getString(R.string.login_tips_registed));
                return;
            }
        }
        //没有注册就进行注册
        UserInfo userInfo = new UserInfo();
        userInfo.account = account;
        userInfo.pwd = pwd;
        userInfo.save();
        CommToast.showToast(mContext, getString(R.string.comm_tips_success));
        finish();
    }
}
